const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


// 封装request
// let baseUrl = 'http://www.liulongbin.top:3005'
let baseUrl = 'http://api.cms.liulongbin.top'

// 封装post的表单请求
const requestpost1=function request(url,data){
  wx.showLoading({
    title: '加载中',
  })
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: baseUrl + url,
      data: data,
      // 处理form表单 的请求头
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        wx.hideLoading();
        resolve(res);
      },
      fail: function (error) {
        wx.hideLoading()
        reject('请求失败')
      }
    })
  });
  return promise
}
// 封装post的json请求
const requestpost2 = function request(url, data) {
  wx.showLoading({
    title: '加载中',
  })
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: baseUrl + url,
      data: data,
      // 处理form表单 的请求头
      header: {
        "Content-Type": "application/json;charset=UTF-8"
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        wx.hideLoading();
        resolve(res);
      },
      fail: function (error) {
        wx.hideLoading()
        reject('请求失败')
      }
    })
  });
  return promise

}

// 封装get的json请求(大多数都是json)
const requestget = function request(url, data) {
  wx.showLoading({
    title: '加载中',
  })
  let promise = new Promise(function (resolve, reject) {
    wx.request({
      url: baseUrl + url,
      data: data,
      // 处理form表单 的请求头
      header: {
        "Content-Type": "application/json;charset=UTF-8"
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        wx.hideLoading();
        resolve(res);
      },
      fail: function (error) {
        wx.hideLoading()
        reject('请求失败')
      }
    })
  });
  return promise
}



module.exports = {
  formatTime: formatTime,
  requestpost1: requestpost1,
  requestpost2: requestpost2,
  requestget: requestget
}
